//
//  BCWalletExchangePayPassController.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2019/1/24.
//  Copyright © 2019 guoshikeji. All rights reserved.
//

#import "BCWalletExchangePayPassController.h"

//
#import "BCWalletExchangeDataModel.h"

//
#import "UserWalletExchangeOprationRequest.h"

@interface BCWalletExchangePayPassController ()


#pragma mark -

@property (nonatomic, strong) BCWalletExchangeDataModel *dataModel;

/**
 *WalletExchangePayComplete
 */
@property (nonatomic, copy) WalletExchangePayComplete completeBlock;

@end

@implementation BCWalletExchangePayPassController

+ (void)showFromController:(UIViewController *)fromController dataModel:(BCWalletExchangeDataModel *)dataModel complete:(WalletExchangePayComplete)complete{
    BControllerWindow *window = [[BControllerWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    //
    BCWalletExchangePayPassController *vc = [BCWalletExchangePayPassController new];
    vc.fromController = fromController;
    vc.showWindow = window;
    vc.dataModel = dataModel;
    vc.completeBlock = complete;
    BCWindowNavigationController *navi = [[BCWindowNavigationController alloc] initWithRootViewController:vc];
    navi.navigationBar.hidden = true;
    navi.navigationBar.barStyle = [vc barStyleFromController:fromController];
    window.rootViewController = navi;
    //
    [window makeKeyAndVisible];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)passwordInputFinishAction {
    [self requestPutPayPassAction];
}

- (void)requestPutPayPassAction {
    [self startLoading];
    UserWalletExchangeOprationRequest *request = [UserWalletExchangeOprationRequest new];
    request.pay_pass = self.passwordTF.text;
    //
    request.from = self.dataModel.fromCurrency.cur_id;
    request.reach = self.dataModel.toCurrency.cur_id;
    //
    request.amount = self.dataModel.amount;
    //
    WEAK_SELF
    [request didRequestCompleted:^{
        [wself stopLoading];
    }];
    [request didRequestSuccessed:^(id result, id page) {
        UserWalletExchangeOprationResponse *resp = result;
        [wself handleSuccess:resp];
    }];
    [request didRequestFailed:^(NSError *error) {
        wself.passwordTF.text = nil;
        if (error&&wself.contentView) {
            NSString *errMsg = error.domain;
            [QMUITips showError:errMsg inView:wself.contentView hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:errMsg]];
        }
    }];
    [request requestServer];
}



- (void)handleSuccess:(UserWalletExchangeOprationResponse *)resp {
    if (self.completeBlock) {
        self.completeBlock(resp);
    }
    [self backButtonDidClickedAction];
}

@end
